package cn.com.css.taxInspection.gt3.service.impl;

import cn.com.css.taxInspection.gt3.entity.DmQxSwrysfEntity;
import cn.com.css.taxInspection.gt3.mapper.DmQxSwrysfMapper;
import cn.com.css.taxInspection.gt3.service.DmQxSwrysfService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DmQxSwrysfServiceImpl extends ServiceImpl<DmQxSwrysfMapper, DmQxSwrysfEntity> implements DmQxSwrysfService {


    @Resource
    private DmQxSwrysfMapper dmQxSwrysfMapper;
    @Override
    public List<DmQxSwrysfEntity> getUserIdentityById(String userId) {
        LambdaQueryWrapper<DmQxSwrysfEntity> lambdaQuery = Wrappers.lambdaQuery();
        return this.list(lambdaQuery.eq(DmQxSwrysfEntity::getSwryDm, userId).eq(DmQxSwrysfEntity::getYxbz, "Y").eq(DmQxSwrysfEntity::getZsfbz, "Y").orderByDesc(DmQxSwrysfEntity::getSwrysfDm));
    }

    @Override
    public DmQxSwrysfEntity getUserIdentityByIdentity(String identity) {
        return this.getById(identity);
    }

    @Override
    public List<DmQxSwrysfEntity> getUserIdentityByIdentityAndTaxorgcode(String identity, String taxorgcode) {
        LambdaQueryWrapper<DmQxSwrysfEntity> lambdaQuery = Wrappers.lambdaQuery();
        return this.list(lambdaQuery.eq(DmQxSwrysfEntity::getSwrysfDm, identity).eq(DmQxSwrysfEntity::getYxbz, "Y").eq(DmQxSwrysfEntity::getSfswjgDm, taxorgcode));

    }

    //根据当前登陆人员税务机关代码，查询身份信息。
    @Override
    public List<DmQxSwrysfEntity> getByUserId(String userId) {
        LambdaQueryWrapper<DmQxSwrysfEntity> lambdaQuery = Wrappers.lambdaQuery();
        return this.list(lambdaQuery.eq(DmQxSwrysfEntity::getSwryDm, userId).eq(DmQxSwrysfEntity::getYxbz, "Y"));
    }

    @Override
    public String getDeptIdByUserId(String userId) {
        String deptId=null;
        LambdaQueryWrapper<DmQxSwrysfEntity> lambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<DmQxSwrysfEntity> queryWrapper = lambdaQuery.eq(DmQxSwrysfEntity::getSwryDm, userId)
                .eq(DmQxSwrysfEntity::getYxbz, "Y")
                .eq(DmQxSwrysfEntity::getZsfbz, "Y");
        DmQxSwrysfEntity dmQxSwrysfEntity = dmQxSwrysfMapper.selectOne(queryWrapper);
        if (TsBqUtils.isNull(dmQxSwrysfEntity)) {
            LambdaQueryWrapper<DmQxSwrysfEntity> lambdaQuery1 = Wrappers.lambdaQuery();
            LambdaQueryWrapper<DmQxSwrysfEntity> queryWrapper1 = lambdaQuery1.eq(DmQxSwrysfEntity::getSwryDm, userId)
                    .eq(DmQxSwrysfEntity::getYxbz, "Y")
                    .orderByDesc(DmQxSwrysfEntity::getLrrq);
            DmQxSwrysfEntity dmQxSwrysfEntity1 = dmQxSwrysfMapper.selectList(queryWrapper1).get(0);
            deptId=dmQxSwrysfEntity1.getSfswjgDm();
        }else {
            deptId=dmQxSwrysfEntity.getSfswjgDm();
        }
        return deptId;
    }


}
